﻿
using System;
using System.ComponentModel;
using System.Web.Mvc;
using HMIS.FrameWork;
using HMIS.Infrastructure.Core;
using HMIS.Infrastructure.Facade;
using HMIS.MVC.CommonSupport;
using HMIS.MVC.ViewModel;


namespace HMIS.MVC.Controllers
{
	//字典管理功能的Controller
	public class InfraDicCategoryController : BaseController
	{
		private readonly IDicCategoryFacade DicCategoryFacade;

		public InfraDicCategoryController(IDicCategoryFacade DicCategoryFacade)
		{
			this.DicCategoryFacade = DicCategoryFacade;
		}

		[Description("访问参数字典管理页面")]
		[ViewPage]
		public ActionResult DicCategory()
		{
			return View();
		}

		[Description("获得参数字典列表")]
		public ActionResult GetPlist()
		{
			long total;
			var data = DicCategoryFacade.GetPlist(
				int.Parse(Request["start"]),
				int.Parse(Request["limit"]),
				Request["sort"],
				Request["dir"],
				this.DesDataFilter(Request["filter"]),
				out total);

			return this.JsonFormat(new ExtGirdData<DicCategory> { total = total, data = data });
		}

		[Description("获得参数字典列表1")]
		public ActionResult GetDicCategoryQueryList()
		{
			long total;
			var data = DicCategoryFacade.QueryPlist(
				Request["query"],
				int.Parse(Request["start"]),
				int.Parse(Request["limit"]),
				out total);

			return this.JsonFormat(new ExtGirdData<DicCategory> { total = total, data = data });
		}

		[ExtResult]
		[Description("添加、修改字典")]
		public ActionResult SaveOrUpdate()
		{
			DicCategory entity;
			if (String.IsNullOrEmpty(Request["Id"]))
			{
				entity = new DicCategory
				{
					Name = Request["Name"],
					TreeCode = Request["TreeCode"],
					//InputCode1 = Request["InputCode1"],
					//InputCode2 = Request["InputCode2"],
					//InputCode3 = Request["InputCode3"],
					Description = Request["Description"]
				};
			}
			else
			{
				entity = this.DicCategoryFacade.GetDicCategory(Request["Id"]);
				entity.Name = Request["Name"];
				entity.TreeCode = Request["TreeCode"];
				//entity.InputCode1 = Request["InputCode1"];
				//entity.InputCode2 = Request["InputCode2"];
				//entity.InputCode3 = Request["InputCode3"];
				entity.Description = Request["Description"];
			}

			DicCategoryFacade.SaveOrUpdate(entity);
			return this.JsonFormat(new ExtResult { success = true, id = entity.Id });
		}

		[ExtResult]
		[Description("删除字典")]
		public ActionResult Delete()
		{
			DicCategoryFacade.DeleteDicCategory(Request["Id"]);
			return this.JsonFormat((new ExtResult { success = true }));
		}

		[ExtResult]
		[Description("判断字典的名称、编码是否在类别下唯一")]
		public ActionResult IsFieldExist()
		{
			var result = DicCategoryFacade.IsFieldExist(Request["FieldName"], Request[Request["FieldName"]], Request["Id"]);
			return this.JsonFormat(new ExtResult { success = !result, msg = "已存在相同的项!" });

		}
	}
}
